using System;
using System.Collections.Generic;
using UnityEngine.UIElements;
using MinesweeperPlus.Scripts.Abstractions.Interfaces;
using MinesweeperPlus.Scripts.Configs;
using MinesweeperPlus.Scripts.Utils;

namespace MinesweeperPlus.Scripts.UI
{
    public sealed class LevelPanel : UIElement
    {
        public new class UxmlFactory : UxmlFactory<LevelPanel> { }

        #region Actions
        public Action OnButtonClick;
        #endregion

        #region Variables
        private LevelConfig _levelConfig;
        private List<IMGUIContainer> _icons;

        private Button _button;
        private Label _nameLabel;
        private Label _sizeLabel;
        private Label _minesLabel;
        private VisualElement _infoPanel;
        #endregion

        #region LevelsPanel Methods
        public LevelPanel() : base() { }
        public LevelPanel(IconsConfig iconsConfig, StyleSheet styleSheet) : 
            base(iconsConfig, styleSheet) { }

        public void SetLevelConfig(LevelConfig levelConfig)
        {
            _nameLabel.text = levelConfig.Name;
            _sizeLabel.text = $"{levelConfig.Width}x{levelConfig.Height}";
            _minesLabel.text = levelConfig.NumberOfMines.ToString();
        }
        #endregion

        #region Other Methods
        protected override void UpdateColorScheme()
        {
            base.UpdateColorScheme();

            Panel.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);
            Panel.SetBorderColor(ColorSchemeConfig.BorderColor);

            _nameLabel.SetTextColor(ColorSchemeConfig.TextColor);
            _sizeLabel.SetTextColor(ColorSchemeConfig.TextColor);
            _minesLabel.SetTextColor(ColorSchemeConfig.TextColor);

            _infoPanel.SetBorderColor(ColorSchemeConfig.BorderColor);

            foreach (var icon in _icons)
                icon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);
        }

        protected override void CreatePanel()
        {
            base.CreatePanel();
            Panel.AddToClassList(Constants.UI_LEVEL_PANEL_STYLE);
            
            _icons = new();

            _button = CreateButton(() => OnButtonClick?.Invoke(), Constants.UI_LEVEL_BUTTON_STYLE);

            var playPanel = CreateVisualElement(Constants.UI_LEVEL_PLAY_PANEL_STYLE);

            var playIcon = CreateIMGUIContainer(
                IconsConfig.EmptyPlayIcon,
                Constants.UI_LEVEL_ICON_STYLE);
            _icons.Add(playIcon);
            playPanel.Add(playIcon);

            _nameLabel = CreateLabel("LevelName");
            playPanel.Add(_nameLabel);

            _infoPanel = CreateVisualElement(Constants.UI_LEVEL_INFO_PANEL_STYLE);

            _sizeLabel = CreateLabel("9x99");
            _infoPanel.Add(_sizeLabel);

            var mineIcon = CreateIMGUIContainer(
                IconsConfig.EmptyMineIcon,
                Constants.UI_LEVEL_ICON_STYLE);
                _icons.Add(mineIcon);
            _infoPanel.Add(mineIcon);

            _minesLabel = CreateLabel("999");
            _infoPanel.Add(_minesLabel);
        }

        private VisualElement CreateVisualElement(string className)
        {
            var visualElement = new VisualElement();
            visualElement.AddToClassList(className);
            _button.Add(visualElement);
            return visualElement;
        }
        private Label CreateLabel(string text = "")
        {
            var label = new Label(text);
            label.AddToClassList(Constants.UI_SMALL_TEXT_STYLE);
            return label;
        }
        #endregion
    }
}
